package com.baojianren.tank;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;

import com.baojianren.bean.Bullet;
import com.baojianren.bean.Tank;

/**
 * 主体窗口运行类
 * 
 * @author ✎ℳ๓₯㎕.倾心❀、
 *
 */
public class TankFrame extends Frame {
	private static final long serialVersionUID = 1L;

	// 绘制坦克设置默认值
	Tank tank = new Tank(200, 200, Dir.DOWN, this);
	public List<Bullet> bullets = new ArrayList<>();
	public Bullet bullet = new Bullet(300, 300, Dir.DOWN,this);
	public static final int GAME_WIDTH = 800, GAME_HEIGHT = 600; // 窗口大小

	/**
	 * 创建窗口主体
	 */
	public TankFrame() {
		// 设置窗口大小
		setSize(GAME_WIDTH, GAME_HEIGHT);
		// 设置窗口左上角图标
		setIconImage(new ImageIcon("src/com/baojiaren/tank/image/logo.png").getImage());
		// 设置窗口启动居中显示
		setLocationFrameSize(this);
		// 设置窗口标题
		setTitle("坦克大战");
		/**
		 * 窗口事件监听
		 */
		addWindowListener(new WindowListener() {

			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			/**
			 * 监听窗口关闭事件
			 */
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);

			}

			@Override
			public void windowClosed(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub

			}
		});
		this.addKeyListener(new MyListener());
		// 设置窗口显示
		this.setVisible(true);
	}

	/**
	 * 重写paint方法 窗口需要重新绘制的时候系统自动调用 Graphics系统画笔
	 * 
	 * @param Graphics
	 */
	@Override
	public void paint(Graphics g) {
		Color c= g.getColor();
		g.setColor(Color.WHITE);
		g.drawString("子弹的速度"+bullets.size(), 10, 60);
		g.setColor(c);
		tank.paint(g);
		// bullet.paint(g);
		for (int i = 0; i < bullets.size(); i++) {
			bullets.get(i).paint(g);
		}
	}

	/**
	 * 设置窗口居中显示
	 * 
	 * @param frame
	 */
	private void setLocationFrameSize(Frame frame) {
		int windowWidth = frame.getWidth(); // 获得窗口宽
		int windowHeight = frame.getHeight(); // 获得窗口高
		Toolkit kit = Toolkit.getDefaultToolkit(); // 定义工具包
		Dimension screenSize = kit.getScreenSize(); // 获取屏幕的尺寸
		int screenWidth = screenSize.width; // 获取屏幕的宽
		int screenHeight = screenSize.height;// 获取屏幕的高
		frame.setLocation(screenWidth / 2 - windowWidth / 2, screenHeight / 2 - windowHeight / 2);// 设置窗口居中显示
	}

	/**
	 * 使用双缓冲解决闪烁问题
	 */
	Image offScreenImage = null;

	@Override
	public void update(Graphics g) {
		if (offScreenImage == null) {
			offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
		}
		Graphics gOffScreen = offScreenImage.getGraphics();
		Color c = gOffScreen.getColor();
		gOffScreen.setColor(Color.BLACK);
		gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
		gOffScreen.setColor(c);
		paint(gOffScreen);
		g.drawImage(offScreenImage, 0, 0, null);
	}

	/**
	 * 键盘监听类
	 * 
	 * @author ✎ℳ๓₯㎕.倾心❀、
	 *
	 */
	class MyListener extends KeyAdapter {
		boolean bL = false;
		boolean bR = false;
		boolean bU = false;
		boolean bD = false;

		/**
		 * 键盘一个按键被按下去的时候调用
		 */
		@Override
		public void keyPressed(KeyEvent e) {
			// 获取键盘值
			int key = e.getKeyCode();
			switch (key) {
			// 键盘方向左键
			case KeyEvent.VK_LEFT:
				bL = true;
				break;
			// 键盘方向上键
			case KeyEvent.VK_UP:
				bU = true;
				break;
			// 键盘方向下键
			case KeyEvent.VK_DOWN:
				bD = true;
				break;
			// 键盘方向右键
			case KeyEvent.VK_RIGHT:
				bR = true;
				break;
			// 键盘方向左键
			case KeyEvent.VK_A:
				bL = true;
				break;
			// 键盘方向上键
			case KeyEvent.VK_W:
				bU = true;
				break;
			// 键盘方向下键
			case KeyEvent.VK_S:
				bD = true;
				break;
			// 键盘方向右键
			case KeyEvent.VK_D:
				bR = true;
				break;
			default:
				break;
			}

			// 设置主战坦克方向
			setMainTankDir();
		}

		/**
		 * 键盘一个按键被抬起来的时候调用
		 */
		@Override
		public void keyReleased(KeyEvent e) {
			// 获取键盘值
			int key = e.getKeyCode();
			switch (key) {
			// 键盘方向左键
			case KeyEvent.VK_LEFT:
				bL = false;
				break;
			// 键盘方向上键
			case KeyEvent.VK_UP:
				bU = false;
				break;
			// 键盘方向下键
			case KeyEvent.VK_DOWN:
				bD = false;
				break;
			// 键盘方向右键
			case KeyEvent.VK_RIGHT:
				bR = false;
				break;
			// 键盘方向左键
			case KeyEvent.VK_A:
				bL = false;
				break;
			// 键盘方向上键
			case KeyEvent.VK_W:
				bU = false;
				break;
			// 键盘方向下键
			case KeyEvent.VK_S:
				bD = false;
				break;
			// 键盘方向右键
			case KeyEvent.VK_D:
				bR = false;
				break;
			// 发射子弹键
			case KeyEvent.VK_X:
				tank.fire();
				break;
			default:
				break;
			}
			// 设置主战坦克方向
			setMainTankDir();
		}

		/**
		 * 创建主战坦克方向
		 */
		private void setMainTankDir() {
			// 如果都没有按方向键设置为禁止移动
			if (!bL && !bU && !bR && !bD) {
				tank.setMoving(false);
			} else {
				// 设置坦克开始移动的值
				tank.setMoving(true);
				if (bL)
					tank.setDir(Dir.LEFT);
				if (bU)
					tank.setDir(Dir.UP);
				if (bR)
					tank.setDir(Dir.RIGHT);
				if (bD)
					tank.setDir(Dir.DOWN);
			}
		}

	}
}
